package com.xkdx.guangguang.fragment.my.setup;

import java.util.HashMap;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.xkdx.caipiao.CompleteUserInfoActivity;
import com.xkdx.caipiao.HomePageActivity;
import com.xkdx.caipiao.R;
import com.xkdx.caipiao.SuggestActivity;
import com.xkdx.caipiao.listener.OnAlertDialogOkListener;
import com.xkdx.caipiao.setup.AboutActivtiy;
import com.xkdx.caipiao.setup.AlterPasswordActivity;
import com.xkdx.caipiao.setup.HelpActivtiy;
import com.xkdx.caipiao.setup.NotificationActivtiy;
import com.xkdx.caipiao.setup.PushActivtiy;
import com.xkdx.caipiao.setup.ShowIdentityInfo;
import com.xkdx.caipiao.wiget.MyAlertDialog;
import com.xkdx.guangguang.LoginActivity;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.my.setup.UpdateVersionModule.Startup;
import com.xkdx.guangguang.fragment.user.UserSharePrefence1;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class SetupFragmentNew extends BaseFragment implements OnClickListener,OnAlertDialogOkListener {
	private Button exit;
	private RelativeLayout userinfo, noticeinfo, pushinfo, versioninfo,
			aboutinfo, adviceinfo, helpinfo,xg_password;
	private UserSharePrefence1 su;
//	private VersionInfoModule module;
//	private VersionInfoAction action;
//	private VersionInfoPresistence presistence;
	
	UpdateVersionPresistener updateVersionPresistener;
	UpdateVerisonAction updateVerisonAction;
	UpdateVersionModule updateVersionModule;
	
	Button setup_back;

	View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_setup_new_caipiao, container, false);
		su = new UserSharePrefence1(getActivity(), IConstants.SP_USER_NEW);
		IConstants.hideInputMethodAways(getActivity(), view);
		updateVerisonAction = new UpdateVerisonAction(getActivity());
		updateVersionModule = new UpdateVersionModule();
		
		findView();
		setOnClick();
		return view;
	}

	public void findView() {
		exit = (Button) view.findViewById(R.id.exit);

		userinfo = (RelativeLayout) view.findViewById(R.id.rl_userinfo);
		noticeinfo = (RelativeLayout) view.findViewById(R.id.rl_noticeinfo);
		pushinfo = (RelativeLayout) view.findViewById(R.id.rl_pushinfo);
		versioninfo = (RelativeLayout) view.findViewById(R.id.rl_versioninfo);
		aboutinfo = (RelativeLayout) view.findViewById(R.id.rl_aboutusinfo);
		adviceinfo = (RelativeLayout) view.findViewById(R.id.rl_adviceinfo);
		helpinfo = (RelativeLayout) view.findViewById(R.id.rl_helpinfo);
		xg_password = (RelativeLayout) view.findViewById(R.id.xg_password);
		setup_back = (Button) view.findViewById(R.id.new_setup_back);
		
		userinfo.setOnClickListener(this);
		noticeinfo.setOnClickListener(this);
		pushinfo.setOnClickListener(this);
		versioninfo.setOnClickListener(this);
		aboutinfo.setOnClickListener(this);
		adviceinfo.setOnClickListener(this);
		exit.setOnClickListener(this);
		helpinfo.setOnClickListener(this);
		xg_password.setOnClickListener(this);
		setup_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_userinfo:
			if (su != null && !"".equals(su.getLogintoken())) {
                if (su.getVerify().equals("1")) {
                    Intent identity = new Intent(getActivity(),
                            ShowIdentityInfo.class);
                    startActivity(identity);
                } else {
                    Intent intent3 = new Intent(getActivity(),
                            CompleteUserInfoActivity.class);
                    startActivity(intent3);
                }

            } else {
            	setToLogin();
            }
			break;
		case R.id.xg_password:
			if (su != null && !"".equals(su.getLogintoken())) {
				Intent identity = new Intent(getActivity(),
						AlterPasswordActivity.class);
				startActivity(identity);
			} else {
				setToLogin();
			}

			
			break;
		case R.id.rl_noticeinfo:
			Intent notice = new Intent(getActivity(),
					NotificationActivtiy.class);
			startActivity(notice);
			break;
		case R.id.rl_pushinfo:
			Intent push = new Intent(getActivity(), PushActivtiy.class);
			startActivity(push);
			break;
		case R.id.rl_versioninfo:
			dataLoadDialog.show();
			
			updateVersionPresistener = new UpdateVersionPresistener(this);
			updateVersionPresistener.setActitons(updateVerisonAction);
			updateVersionPresistener.setModule(updateVersionModule);
			updateVersionPresistener.execute();
			break;
		case R.id.rl_aboutusinfo:
			Intent about = new Intent(getActivity(), AboutActivtiy.class);
			startActivity(about);
			break;
		case R.id.rl_adviceinfo:
			Intent intent = new Intent(getActivity(), SuggestActivity.class);
			startActivity(intent);
			break;
		case R.id.rl_helpinfo:
			Intent help = new Intent(getActivity(), HelpActivtiy.class);
			startActivity(help);
			break;
		case R.id.new_setup_back:
			getActivity().finish();
			break;
		case R.id.exit:
			if (su != null) {
				showExit();
			} else {
				Intent login = new Intent(getActivity(), LoginActivity.class);
				startActivity(login);
			}
			break;

		}
	}

	private void showExit() {
		MyAlertDialog.creatAlertDialog(getActivity(), "退出提示", "确定退出账号?");
        MyAlertDialog.setOnAlertDialogOklistener(this);
        SharePrefenceUtil sp = new SharePrefenceUtil(getActivity(), IConstants.SP_USER);
        sp.clear();
        su.clear();
        com.xkdx.caipiao.statics.IConstants.list_selectcode.clear();
        com.xkdx.caipiao.statics.IConstants.list_sevencolor.clear();
        com.xkdx.caipiao.statics.IConstants.list_three.clear();
        com.xkdx.caipiao.statics.IConstants.list_five.clear();

	}

	private void download(String clientDownloadUrl) {
		IConstants.download(getActivity(), clientDownloadUrl);
	}
	
	public void handleOkClick() {
		su.clear();
//		exit.setVisibility(View.GONE);
		Intent inent = new Intent(getActivity(), HomePageActivity.class);
		inent.putExtra("tag", "setup");
		startActivity(inent);
	}
	
	private void setToLogin() {
		Intent intent = new Intent(getActivity(), LoginActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

		startActivity(intent);

	}

	@Override
	public void setOnClick() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showLoading() {
		
	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			final Startup startup = updateVersionModule.startup;
			if (startup.getClientUpdate().equals("True")) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						getActivity());
				builder.setTitle("有新版本,是否更新");
				builder.setNegativeButton("是",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								download(startup.getClientDownloadUrl());
							}
						});
				builder.setPositiveButton("否", null);
				builder.create().show();
			} else {
				Toast.makeText(getActivity(), "已经是最新版本", Toast.LENGTH_SHORT)
						.show();
			}

		} else {
			showErrorTip(map);
		}
		
	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void handleDismissClick() {
		// TODO Auto-generated method stub
		
	}
}
